﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.AuthorityManageForms.UserForms
{
    public partial class LoginUserMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.LoginUserBll loginUserBll = new BLL.LoginUserBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        public LoginUserMainForm()
        {
            InitializeComponent();
        }
        private void LoginUserMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

            List<Role> roleList = toolBll.GetRoleList();
            this.repositoryItemLookUpEdit1.DataSource = roleList;
            InitGrid();
        }

        private void InitGrid()
        {
            var models = loginUserBll.LoadEntitiesAsNoTracking(m => m.IsDelete == false).OrderByDescending(m => m.LoginUserID).ToList();
            this.gridControl1.DataSource = models;
        }
        private void ButtonItem_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoginUserDetailForm loginUserDetailForm = new LoginUserDetailForm(InitGrid);
            loginUserDetailForm.Show();
        }

        private void ButtonItem_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("LoginUserID"));
            LoginUser user = loginUserBll.LoadEntities(m => m.LoginUserID == id).FirstOrDefault();
            if (user != null)
            {
                LoginUserDetailForm loginUserDetailForm = new LoginUserDetailForm(user, InitGrid);
                loginUserDetailForm.Show();
            }

        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("LoginUserID"));
                LoginUser user = db.LoginUsers.Find(id); ;
                if (user != null)
                {
                    DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        user.IsDelete = true;
                        bool res = db.SaveChanges() > 0;
                        if (res)
                        {
                            InitGrid();
                            XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                        }
                    }
                }
            }
        }


    }
}